package com.iptv.grape.jss.mgr;

import android.webkit.WebView;

import com.iptv.grape.BaseAct;
import com.iptv.grape.beans.WebBean;
import com.iptv.grape.jss.impls.listener.VideoCallBack;
import com.iptv.grape.jss.impls.listener.WebCallBack;
import com.iptv.grape.jss.impls.Deliver;
import com.iptv.grape.jss.impls.ImplFactory;
import com.iptv.grape.video.VideoSize;
import com.iptv.grape.video.XMediaPlayer;

public class CallBackMgr implements WebCallBack.WebBackImpl, VideoCallBack.VideoBackImpl, Deliver {
    private WebView webBiew = null;
    private XMediaPlayer mediaPlayer = null;
    private BaseAct mAct;
    private static CallBackMgr callBackMgr;
    public static CallBackMgr istc(){
        if(null==callBackMgr){
            synchronized (CallBackMgr.class){
                if(null==callBackMgr)
                    callBackMgr = new CallBackMgr();
            }
        }
        return  callBackMgr;
    }

    @Override
    public void init(BaseAct mAct,WebView webView,XMediaPlayer mediaPlayer){
        this.mAct = mAct;
        this.webBiew = webView;
        this.mediaPlayer = mediaPlayer;
    }

    @Override
    public void recvWebImpl(WebBean bean) {
        String tag = bean.tag;
        switch (tag) {
            case WebCallBack.TAG_WEB_openBST:
                String contentID = bean.param.contentID;
                break;

            case WebCallBack.TAG_WEB_quitActivity:

                break;
            case WebCallBack.TAG_WEB_jumpWebUrl:
                String openType = bean.param.openType;
                if (WebCallBack.TAG_OEPN_URL_TYPE_WEB.equals(openType)) {

                }
                break;
        }
    }

    @Override
    public void playUrlVideoImpl(String url) {
        if (mediaPlayer.isPlaying()) {
            // 如过正在播放点播
            mediaPlayer.stop();
        }
        mediaPlayer.playVideo(mAct, url);
    }

    @Override
    public void playUrlVideoImpl(String url, int left, int top, int width, int height, boolean isLoop) {
        VideoSize videoSize = new VideoSize(width, height, left, top);
        mediaPlayer.play(mAct, url, videoSize, isLoop);

    }

    @Override
    public void setPositionImpl(int left, int top, int width, int height) {
        VideoSize videoSize = new VideoSize(width, height, left, top);
        mediaPlayer.setPosition(videoSize);
    }

    @Override
    public void playFullScreenImpl() {
        mediaPlayer.fullScreen();
    }

    @Override
    public void pauseImpl() {
        mediaPlayer.pause();
    }

    @Override
    public void playImpl() {
        mediaPlayer.start();
    }

    @Override
    public void seekImpl(int sec) {
        mediaPlayer.seekTo(sec);
    }

    @Override
    public void closeVideoImpl() {
        mediaPlayer.stop();
    }

    @Override
    public boolean isPlayingImpl() {
        return mediaPlayer.isPlaying();
    }

    @Override
    public int getTotalTimeImpl() {

        return mediaPlayer.getDuration();
    }

    @Override
    public int getCurrentTimeImpl() {
        return mediaPlayer.getCurrentPosition();
    }



}
